﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;

public partial class Management_Company_Monthly : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            panel_Date.Visible = false;
        }
    }
    protected void btn_ExpirationDate_Click(object sender, EventArgs e)
    {
        panel_Date.Visible = true;
        var _btn = sender as Button;
        txt_Company.Value = _btn.CommandArgument;
    }
    protected void btn_Set_Click(object sender, EventArgs e)
    {
        try
        {
            DateTime _date = DateTime.MinValue;
            try
            {
                _date = Convert.ToDateTime(txt_ExDate.Text);
            }
            catch (Exception)
            {
                throw new Exception("日期输入格式错误，请重新输入。");
            }
            if (_date <= DateTime.Today) throw new Exception("日期必须大于今日。");

            var _company = txt_Company.Value;
            using (var _adc = DBHelper.GetDataContext())
            {
                var _exDate = _adc.CompanyMonthlyUserExpirationDate.FirstOrDefault(d => d.Company == _company);
                if (_exDate != null)
                {
                    _exDate.ExpirationDate = _date;
                }
                else
                {
                    _exDate = new CompanyMonthlyUserExpirationDate();
                    _exDate.Company = _company;
                    _exDate.ExpirationDate = _date;

                    _adc.CompanyMonthlyUserExpirationDate.InsertOnSubmit(_exDate);
                }
                _adc.SubmitChanges();
            }
            GridView1.DataBind();
            panel_Date.Visible = false;
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "错误:"+_e.Message;
        }
    }
}
